package task;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        queue.take().run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }
    public void submit(Runnable runnable) {
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Test8 {
    public static void main(String[] args) {
        MyThreadPool pool = new MyThreadPool(5);
        for (int i = 0; i < 1000; i++) {
            int id = i;
            pool.submit(() ->{
                System.out.println("id="+id +" "+Thread.currentThread().getName());
            });
        }
    }
}
